import { takeEvery, takeLatest, throttle,select,call,take,put, fork,actionChannel,call } from "redux-saga/effects"
import axios from 'axios';
const jumpUrl =(jurl)=>{
    window.location.url = jurl;
}
export function* defSaga(){
    console.log("saga 初始化开始");
    function* watchAndLog(){
        yield takeEvery("*",function* logger(action){
            const state = yield select();
            console.log("action",action);
            console.log("state after",state);
        })
    }
    function* watchAndLog(){
        while(true){
            const action = yield take("*")
            const state = yield select()
            console.log("action",action);
            console.log("state after",state);
        }
    }
    yield fork(function* fetchLogin(){
        try{
            const res = yield call(axios.post,"http://localhost:5656/login")
            console.log("10 10",res);
            if(res){
                yield put({type:'UPDATE_USER',payload:res.data});
                yield watchAndLog();
            }else{
                // 模拟
                yield put(push('/403'));
            }
        }catch(e){
            //yield put(jumpUrl({""});
            console.warn("500");
        }
    });
    yield takeEvery("takeEvery",function*(){
        const user = yield select(state=>state.user);
        const res = yield call(axios.post,"http://localhost:5656/login",{
            ...user
        })
        yield put({
            type:"login_success",
            ...res.data
        })
        console.log("takeEvery",res);
    })
    yield takeLatest('takeLatest',function*(){
        const user = yield select(state=>state.user);
        const res = yield call(axios.post,"http://localhost:5656/login",{
            ...user
        })
        console.log("takeLatest",res);
    })
    yield throttle(2000,"throttle",function*(){
        const user = yield select(state=>state.user);
        const res = yield call(axios.post,"http://localhost:5656/login",{
            ...user
        })
        console.log("throttle",res);
    })
}